package cn.wolfcode.luowowo.website.web.controller;

import cn.wolfcode.luowowo.article.domain.Destination;
import cn.wolfcode.luowowo.article.domain.StrategyContent;
import cn.wolfcode.luowowo.article.domain.StrategyDetail;
import cn.wolfcode.luowowo.article.domain.StrategyTag;
import cn.wolfcode.luowowo.article.query.StrategyDetailQuery;
import cn.wolfcode.luowowo.article.service.*;
import cn.wolfcode.luowowo.cache.service.IStrategyStatisRedisService;
import cn.wolfcode.luowowo.cache.vo.StrategyStatisVO;
import cn.wolfcode.luowowo.common.util.AjaxResult;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.search.query.SearchQueryObject;
import cn.wolfcode.luowowo.search.service.IStrategyEsService;
import cn.wolfcode.luowowo.search.vo.StatisVO;
import cn.wolfcode.luowowo.website.web.annotation.UserParam;
import cn.wolfcode.luowowo.comment.dumain.StrategyComment;
import cn.wolfcode.luowowo.comment.query.StrategyCommentQuery;
import cn.wolfcode.luowowo.comment.service.IStrategyCommentService;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/strategy")
public class StrategyController {

    @Reference
    private ITravelService travelService;

    @Reference
    private IDestinationService destinationService;

    @Reference
    private IStrategyTagService strategyTagService;

    @Reference
    private IStrategyDetailService strategyDetailService;

    @Reference
    private IStrategyCommentService strategyCommentService;

    @Reference
    private IStrategyStatisRedisService strategyStatisRedisService;

    @Reference
    private IStrategyCommendService strategyCommendService;

    @Reference
    private IStrategyEsService strategyEsService;


    @RequestMapping("")
    public String index(Model model){
        // commends
        model.addAttribute("commends", strategyCommendService.getTop5());

        // hotCds热门
        List<StrategyStatisVO> HotList = strategyStatisRedisService.getHotRankingTop10();
        model.addAttribute("hotCds", HotList);

        // unabroadCds国内
        List<StrategyStatisVO> chinaList = strategyStatisRedisService.getChinaRankingTop10();
        model.addAttribute("unabroadCds", chinaList);

        // abroadCds国外
        List<StrategyStatisVO> abroadList = strategyStatisRedisService.getAbroadRankingTop10();
        model.addAttribute("abroadCds", abroadList);

        // 主题推荐
        // themeCds
        List<Map<String, Object>> data = strategyEsService.queryThemeCommend();
        model.addAttribute("themeCds", data);

        // chinas
        List<StatisVO> chinas = strategyEsService.queryConditionChina();
        model.addAttribute("chinas", chinas);

        // abroads
        List<StatisVO> abroads = strategyEsService.queryConditionAbroad();
        model.addAttribute("abroads", abroads);

        // themes
        List<StatisVO> themes = strategyEsService.queryConditionTheme();
        model.addAttribute("themes", themes);


        return "strategy/index";
    }

    @RequestMapping("/detail")
    public String detail(Model model, Long id){

        StrategyDetail detail = strategyDetailService.get(id);
        StrategyContent content = strategyDetailService.getContentById(id);
        detail.setStrategyContent(content);

        model.addAttribute("detail", detail);

        // 阅读数 + 1
        strategyStatisRedisService.addViewnum(id, 1);
        StrategyStatisVO vo = strategyStatisRedisService.getStrategyStatisVO(id);
        model.addAttribute("vo", vo);

        // 热门排行加分 + 1
        strategyStatisRedisService.addHotScore(id, 1);

        return "strategy/detail";
    }

    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") StrategyDetailQuery qo){

        // toasts
        List<Destination> toasts = destinationService.getToasts(qo.getDestId());
        Destination dest = toasts.remove(toasts.size() - 1);
        model.addAttribute("toasts", toasts);
        model.addAttribute("dest", dest);

        // tags
        List<StrategyTag> tags = strategyTagService.list();
        model.addAttribute("tags", tags);

        PageInfo page = strategyDetailService.query(qo);
        model.addAttribute("pageInfo", page);


        return "strategy/list";
    }

    /*detailId: 2
    content: 好棒
    detailTitle: 有娃必看，广州长隆野生动物园全攻略*/
    // 攻略明细评论
    @RequestMapping("/commentAdd")
    @ResponseBody
    public Object commentAdd(StrategyComment comment, @UserParam UserInfo userInfo){
        if(userInfo == null){
            return new AjaxResult(false, "请先登陆");
        }
        comment.setUsername(userInfo.getNickname());
        comment.setUserId(userInfo.getId());
        comment.setCity(userInfo.getCity());
        comment.setLevel(userInfo.getLevel());
        comment.setHeadUrl(userInfo.getHeadImgUrl());

        strategyCommentService.save(comment);

        // 评论数
        strategyStatisRedisService.addReplynum(comment.getDetailId(), 1);
        StrategyStatisVO vo = strategyStatisRedisService.getStrategyStatisVO((comment.getDetailId()));

        // 热门排行加分 + 1
        strategyStatisRedisService.addHotScore(comment.getDetailId(), 1);

        return AjaxResult.SUCCESS.addData(vo.getReplynum());
    }

    /*currentPage: 1
    detailId: 2*/
    //游记评论回显
    @RequestMapping("/comment")
    public String comment(Model model, @ModelAttribute("qo") StrategyCommentQuery qo){
        Page page = strategyCommentService.query(qo);
        model.addAttribute("page", page);
        return "strategy/commentTpl";
    }

   // toid: 5e7b35dcc35564197ced6616
    //fromid: 4
    // 攻略明细评论点赞
    @RequestMapping("/commentThumbUp")
    @ResponseBody
    public Object commentThumbUp(String toid, @UserParam UserInfo userInfo){

        strategyCommentService.commentThumbUp(toid, userInfo.getId());

        return AjaxResult.SUCCESS;
    }

    // 收藏
    @RequestMapping("/favor")
    @ResponseBody
    public Object favor(Long sid, @UserParam UserInfo userInfo){
        if(userInfo == null){
            AjaxResult ajax = new AjaxResult(false, "请先登陆");
            ajax.setCode(102);
            return ajax;
        }
        // 约定: 如果ret true表示收藏, 如果ret false取消收藏
        boolean ret = strategyStatisRedisService.favor(sid, userInfo.getId());
        StrategyStatisVO vo = strategyStatisRedisService.getStrategyStatisVO(sid);

        // 国内外排行加分
        strategyStatisRedisService.addAbroadOrChina(sid, ret?1:-1);

        return new AjaxResult(ret).addData(vo);
    }

    @RequestMapping("/strategyThumbup")
    @ResponseBody
    public Object strategyThumbup(Long sid, @UserParam UserInfo userInfo){
        if(userInfo == null){
            AjaxResult ajax = new AjaxResult(false, "请先登陆");
            ajax.setCode(102);
            return ajax;
        }
        // 约定: 如果ret true表示顶成功, 如果ret false已经顶过
        boolean ret = strategyStatisRedisService.thumbup(sid, userInfo.getId());
        StrategyStatisVO vo = strategyStatisRedisService.getStrategyStatisVO(sid);

        // 国内外排行加分
        strategyStatisRedisService.addAbroadOrChina(sid, 1);

        return new AjaxResult(ret, "").addData(vo);
    }

    @RequestMapping("/searchPage")
    public String searPage(Model model, @ModelAttribute("qo") SearchQueryObject qo){
        Page page = strategyEsService.query(qo);
        model.addAttribute("page", page);

        return "strategy/searchPageTpl";
    }

}
